﻿namespace SLClient.CustomerControls
{
    using System;
    using System.Collections.Generic;

    public class PL
    {
        public string Data = "";
        public List<string> Datas = null;

        public PL(string data)
        {
            this.Data = data;
            this.Datas = new List<string>();
            this.BackTrack(0);
        }

        private void BackTrack(int i)
        {
            if (i >= this.Data.Length)
            {
                this.Datas.Add(this.Data);
            }
            for (int j = i; j < this.Data.Length; j++)
            {
                if (this.IsOk(i, j))
                {
                    this.Swap(ref this.Data, i, j);
                    this.BackTrack(i + 1);
                    this.Swap(ref this.Data, i, j);
                }
            }
        }

        private bool IsOk(int i, int j)
        {
            if (i < j)
            {
                for (int k = i; k < j; k++)
                {
                    if (this.Data[k] == this.Data[j])
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        public static string SortString(string data)
        {
            char[] array = data.ToCharArray();
            Array.Sort<char>(array);
            return new string(array);
        }

        private void Swap(ref string str, int i, int j)
        {
            if ((((i >= 0) && (j >= 0)) && ((i < str.Length) && (j < str.Length))) && (i != j))
            {
                char[] chArray = str.ToCharArray();
                char ch = chArray[i];
                chArray[i] = chArray[j];
                chArray[j] = ch;
                str = new string(chArray);
            }
        }

        public int Count
        {
            get
            {
                return this.Datas.Count;
            }
        }
    }
}

